import unittest
import requests
import os, sys
parentdir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, parentdir)
import xlrd
from xlutils.copy import copy
from xlrd import open_workbook
import time


class Api_test(unittest.TestCase):
    '''影片详情接口情况'''

    def setUp(self):
        self.now = time.strftime("%Y-%m-%d %H_%M_%S")
        self.filenane = './parameter/' + self.now + '_result.xls'
        try:
            self.work = xlrd.open_workbook(parentdir + '\parameter\param.xlsx')

        except:
            print('无没有存在参数文件！')
            exit('退出程序，请添加参数文件！')
        self.sheetName=self.work.sheet_by_name('param')
        data = self.sheetName.row_values( 2)
        self.base_url =str(data[0])
        self.requests_method=str(data[1])
        self.Trowcount=str(data[2])
        self.Tcolumncount = str(data[3])
        #获取toke
        self.token=str(data[4])
        self.contentType=str(data[5])
        if self.requests_method=='delete':
            self.headers = {
                "Content-Type": self.contentType,
                "Authorization": self.token
            }
        else:
            self.header={
                "Content-Type":self.contentType,
                "token":self.token
            }

    def tearDown(self):
        pass

    def test_ApiRemindDeliveryList(self):
        '''检测修改影片信息接口'''
        i=0
        c=0
        #获取参数名称，固定行数
        d_name = self.sheetName.row_values(4)
        #定义一个空的数据，存获取参数的名称
        x_data = []
        #定义一个空的数据，获取参数相对于的值
        list = []
        #循环获取固定行数的参数并转为数组存储
        for c in range(round(float(self.Tcolumncount))):
            x_data.append(d_name[c])
        #循环获取每一行参数的值
        for i in range(round(float(self.Trowcount))):
            data = self.sheetName.row_values(i + 6)
            #定义一个空的字典，存储{参数名：值}
            tmp = {}

            #循环获取相对于的参数与相应的值
            for j in range(len(x_data)):
                #转换获取参数的类型
                if isinstance(data[j],float):
                    data[j]=round(float(data[j]))
                else:
                    data[j]=str(data[j])
                tmp[x_data[j]] = data[j]
                list.append(tmp)
            if self.requests_method=='get':
                r = requests.get(self.base_url, params=tmp,headers=self.header)
                self.result = r.json()
            elif self.requests_method=='put':
                r = requests.put(self.base_url, params=tmp,headers=self.header)
                self.result = r.json()
            elif self.requests_method=='delete':
                r=requests.delete(self.base_url,params=tmp,headers=self.headers)
                print(r.request.headers)
                print(r.request.body)
                self.result = r.json()

            else:
                r = requests.post(self.base_url, data=tmp,headers=self.header)
                self.result = r.json()

            print(self.result)
            yuqi=data[round(float(self.Tcolumncount)) + 1]
            if isinstance(yuqi,float):
                yuqi=round(float(yuqi))
            else:
                yuqi = str(yuqi)

            #有些系统的返回码名称为self.result["code"]
            if self.result["state"]==yuqi:
                #print(self.result['msg']['total'])
                print("用例"+str(i+1)+"："+str(data[round(float(self.Tcolumncount))])+",预期结果："+str(yuqi)+",实际结果：pass")
                pass
            elif self.result["state"]==400:
                print("用例" +str(i+1)+"："+ str(data[round(float(self.Tcolumncount))]) + ",预期结果：" + str(
                    yuqi) + ",实际结果：False")
                False
            else:
                print("用例" + str(i + 1)+"：" + str(data[round(float(self.Tcolumncount))]) + ",预期结果：" + str(
                    yuqi) + ",实际结果：False")
                False









if __name__=='__main__':
    untitest.main()
